import Vue from 'vue'
import Vuex from 'vuex'
import state from './state'
import axios from 'axios';

Vue.use(Vuex)

const debug = process.env.NODE_ENV !== 'production'

const mutations={
	first_init(state,obj){
			//读取保存在localStorage对象里名为obj.url的变量的值
			if(localStorage.getItem(obj.url)){
				//从本地存储-读取数据localStorage.getItem
				state.newList=JSON.parse(localStorage.getItem(obj.url))
			}else{
				//如果没有本地储存就esaymock获取数据
				axios({
				methods:'get',
				url:obj.url,
				params:{
					rid:state.count
				}
			}).then((res)=>{
				console.log(res)
				state.newList = res.data.data;
                //存储数据 localStorage.setItem
				localStorage.setItem(obj.url,JSON.stringify(res.data.data))
			})	
		}
	},
	_init(state,obj){
		axios({
			method:'get',
			url:obj.url,
			params:{
				rid:obj.n
			}
		}).then((res)=>{
			state.contentList = res.data.data
			const listNum = state.contentList.length;
			let con = state.contentList.splice(0,listNum)
			state.newList = state.newList.concat(con)
		})
	},
	add(state){
		state.count++
	},
	recover(state){
		state.count = 1
//		console.log(1)
	}
}

export default new Vuex.Store({
	state,
	mutations
})
